package com.baosight.risk.service.quartz.impl;

import com.baosight.risk.api.quartz.ISysJobService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.utils.CronUtils;
import com.baosight.risk.common.utils.ScheduleConstants;
import com.baosight.risk.common.utils.ScheduleUtils;
import com.baosight.risk.mapper.quartz.SysJobMapper;
import com.baosight.risk.service.quartz.entity.SysJob;
import net.sf.json.JSONArray;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author duanyongqiang
 * <p>
 * 定时任务管理
 */
@Service
public class SysJobServiceImpl extends BaseServiceImpl<SysJob> implements ISysJobService {
    @Autowired
    private SysJobMapper jobMapper;

    @Autowired
    @Qualifier(value = "schedulerFactoryBean")
    private Scheduler scheduler;

    @Override
    public BaseMapper getMapper() {
        return jobMapper;
    }

    @Override
    public List<SysJob> selectJob(SysJob job) {
        return jobMapper.selectJob(job);
    }


    /**
     * 任务调度状态修改
     *
     * @param job 调度信息
     */
    @Override
    public int changeStatus(SysJob job) {
        int rows = 0;
        String status = job.getStatus();
        if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
            rows = resumeJob(job);
        } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
            rows = pauseJob(job);
        }
        return rows;
    }

    /**
     * 暂停任务
     *
     * @param job 调度信息
     */
    @Override
    public int pauseJob(SysJob job) {
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = jobMapper.updateByPrimaryKeySelective(job);
        if (rows > 0) {
            ScheduleUtils.pauseJob(scheduler, job.getId());
        }
        return rows;
    }

    /**
     * 恢复任务
     *
     * @param job 调度信息
     */
    @Override
    public int resumeJob(SysJob job) {
        job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        int rows = jobMapper.updateByPrimaryKeySelective(job);
        if (rows > 0) {
            ScheduleUtils.resumeJob(scheduler, job.getId());
        }
        return rows;
    }


    /**
     * 立即运行任务
     *
     * @param job 调度信息
     */
    @Override
    public int run(SysJob job) {
        return ScheduleUtils.run(scheduler, job);
    }


    /**
     * 新增任务
     *
     * @param job 调度信息 调度信息
     */
    @Override
    public int insertJobCron(SysJob job) {
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = jobMapper.insert(job);
        if (rows > 0) {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
        return rows;
    }

    /**
     * 更新任务的时间表达式
     *
     * @param job 调度信息
     */
    @Override
    public int updateJobCron(SysJob job) {
        int rows = update(job);
        if (rows > 0) {
            ScheduleUtils.updateScheduleJob(scheduler, job);
        }
        return rows;
    }

    /**
     * 更新任务的执行参数
     *
     * @param job 调度信息
     */
    @Override
    public int updateJobParam(SysJob job) {
        return jobMapper.updateByPrimaryKeySelective(job);
    }

    /**
     * 校验cron表达式是否有效
     *
     * @param cronExpression 表达式
     * @return 结果
     */
    @Override
    public boolean checkCronExpressionIsValid(String cronExpression) {
        return CronUtils.isValid(cronExpression);
    }


    /**
     * 批量删除调度信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public void deleteJobByIds(List<String> ids) {
        for (String jobId : ids) {
            SysJob job = this.get(jobId);
            deleteJob(job);
        }
    }

    /**
     * 删除任务后，所对应的trigger也将被删除
     *
     * @param job 调度信息
     */
    @Override
    public int deleteJob(SysJob job) {
        int rows = jobMapper.delete(job);
        if (rows > 0) {
            ScheduleUtils.deleteScheduleJob(scheduler, job.getId());
        }
        return rows;
    }
}
